package org.electrical.controller.functionality;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.electrical.dao.EleCustomerDao;
import org.electrical.pojo.EleCustomer;
import org.electrical.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
@CrossOrigin(value = "*")
public class EleCustomerController {

    @Autowired
    private EleCustomerDao eleCustomerDao;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<EleCustomer>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, EleCustomer condition) throws Exception {

        Page<EleCustomer> pager = PageHelper.startPage(page, limit);
        List<EleCustomer> list = eleCustomerDao.getConditionList(condition);
        return new ResponseResult(pager.getTotal(), list);

    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(EleCustomer eleCustomer) throws Exception {

        eleCustomerDao.update(eleCustomer);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/regress")
    @ResponseBody
    public ResponseResult regress(String cusId) throws Exception {

        eleCustomerDao.regress(cusId);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/early")
    @ResponseBody
    public ResponseResult early(String cusId) throws Exception {

        eleCustomerDao.early(cusId);
        return ResponseResult.createSuccessResult();

    }

    /**
     * 暂缓流失
     */
    @RequestMapping("/measures")
    @ResponseBody
    public ResponseResult measures(String cusId) throws Exception {

        eleCustomerDao.measures(cusId);
        return ResponseResult.createSuccessResult();

    }

    /**
     * 确认流失
     */
    @RequestMapping("/reason")
    @ResponseBody
    public ResponseResult reason(String cusId) throws Exception {

        eleCustomerDao.reason(cusId);
        return ResponseResult.createSuccessResult();

    }

    /**
     * 统计
     */
    @RequestMapping("/level")
    @ResponseBody
    public ResponseResult level() throws Exception {

        List<EleCustomer> list = eleCustomerDao.getByLevel();
        return new ResponseResult(list);

    }

    @RequestMapping("/satisfied")
    @ResponseBody
    public ResponseResult satisfied() throws Exception {

        List<EleCustomer> list = eleCustomerDao.getBySatisfied();
        return new ResponseResult(list);

    }

    @RequestMapping("/credit")
    @ResponseBody
    public ResponseResult credit() throws Exception {

        List<EleCustomer> list = eleCustomerDao.getByCredit();
        return new ResponseResult(list);

    }

}
